#!/usr/bin/env python
# -*- coding: utf-8 -*-

from tornado.web import RequestHandler
import pymongo
import re
import render
import codecs
import _clientCache as clientcache
import sys
sys.path.insert(0,'/home/leeray/workspaces/Python/wanalytics/')
sys.path.insert(0,'/opt/web/')
from Tools import connectMySQL 
import json

_encoding = 'utf-8'
_errors='ignore'
color = ['AFD8F8','F6BD0F','8BBA00','FF8E46','008E8E','D64646','8E468E','588526','B3AA00','008ED6','9D080D','A186BE','AFD8F8','F6BD0F','8BBA00','FF8E46','008E8E','D64646','8E468E','588526','B3AA00','008ED6','9D080D','A186BE','AFD8F8','F6BD0F','8BBA00','FF8E46','008E8E','D64646','8E468E','588526','B3AA00','008ED6','9D080D','A186BE',]
client_pid = {}
android_phone_pid = []
android_pad_pid = []

def add(x, y):
    return x + y

class PV(RequestHandler):
    def get(self): 
        _time = self.get_argument('time', '').strip()
        _platform = self.get_argument('platform', '').strip()
        if not _time:
            _time = '201110'
        if not _platform:
            _platform = 'all'
        connection = pymongo.Connection("10.103.13.42", 27017)
        db = connection.DailyLog
        collection = db[_time]
        posts = collection.find()

        print dir(posts)
        _posts = []
        pv = {}
        compiles_android = re.compile('android', re.I)
        compiles_ios = re.compile('(ios|iphone|ipad)', re.I)
        for post in posts:
            pid = post['pid']
            if pid == '127f7a5e3a366bd0' or pid == '53ef315e3bad9e03' or pid == 'a8c022bcd2adcbee':
                continue
            if _platform == 'android':
                print post.get('keyword','')
                datamatch = compiles_android.search(post.get('keyword',''))
                if datamatch:
                    pv[post['date']] = pv.get(post['date'], 0) + post['pv']
            elif _platform == 'ios':
                datamatch = compiles_ios.search(post.get('keyword',''))
                if datamatch:
                    pv[post['date']] = pv.get(post['date'], 0) + post['pv']
            elif _platform == 'wap':
                if 'wap' == post['server']:
                    pv[post['date']] = pv.get(post['date'], 0) + post['pv']
            else:
                pv[post['date']] = pv.get(post['date'], 0) + post['pv']
           
        print pv
#        for a in pv.items():
#            print dir(a)
        lpv = list(pv)
        lpv.sort()
        
        #xmlStr = render.xml(pv)
        xml_head=ur"<graph caption=' {0}年{1}月 PV数据报表图' xAxisName='Day' yAxisName='PV' \
        showValues='1' lineThickness='1' animation='1' alpha='100' showLimits='1' decimalPrecision='1' rotateNames='1' numDivLines='3'\
        bgcolor='F3f3f3' bgAlpha='70' \
        showColumnShadow='0' divlinecolor='c5c5c5' divLineAlpha='80' \
        showAlternateHGridColor='1' alternateHGridColor='f8f8f8' alternateHGridAlpha='80' > ".format(_time[:4], _time[4:])
        xml_content = ''
        for i in range(len(lpv)):
            xml_content += "<set name='{0}' value='{1}' color='{2}'/>".format(lpv[i], pv[lpv[i]], color[i])
        xml_footer = '''</graph>'''
        xmlStr = xml_head+xml_content+xml_footer
        
        self.render('templates/client-pv.html', content=pv, keys=lpv, add=add, xmlStr=xmlStr, time=_time, platform=_platform )

class UV(RequestHandler):
    def get(self): 
        _time = self.get_argument('time', '').strip()
        _platform = self.get_argument('platform', '').strip()
        if not _time:
            _time = '201111'
        if not _platform:
            _platform = 'all'
        connection = pymongo.Connection("10.103.13.42", 27017)
        db = connection.DailyLog
        collection = db[_time]
        posts = collection.find()

        print dir(posts)
        _posts = []
        uv1 = {}
        uv2 = {}
        compiles_android = re.compile('android', re.I)
        compiles_ios = re.compile('(ios|iphone|ipad)', re.I)
        compiles_other = re.compile('^((?!android|ios|iphone|ipad|wap).)*$', re.I)
        for post in posts:
            pid = post['pid']
            
            if _platform == 'android':
                datamatch = compiles_android.search(post.get('keyword',''))
                if datamatch:
                    uv1[post['date']] = uv1.get(post['date'], 0) + post.get('uv1',0)
                    uv2[post['date']] = uv2.get(post['date'], 0) + post.get('uv2',0)
            elif _platform == 'ios':
                datamatch = compiles_ios.search(post.get('keyword',''))
                if datamatch:
                    uv1[post['date']] = uv1.get(post['date'], 0) + post.get('uv1',0)
                    uv2[post['date']] = uv2.get(post['date'], 0) + post.get('uv2',0)
            elif _platform == 'wap':
                if 'wap' in post.get('keyword',''):
                    uv1[post['date']] = uv1.get(post['date'], 0) + post.get('uv1',0)
                    uv2[post['date']] = uv2.get(post['date'], 0) + post.get('uv2',0)
            elif _platform == 'other':
                keyword = post.get('keyword','')
                datamatch = compiles_other.search(keyword)
                if not keyword  or datamatch:
                    print 'keyword:{0}, pid:{1}, uv1:{2}, uv2:{3}, pv:{4}'.format(keyword, post['pid'], post.get('uv1',0), post.get('uv2',0), post.get('pv',0))
                    uv1[post['date']] = uv1.get(post['date'], 0) + post.get('uv1',0)
                    uv2[post['date']] = uv2.get(post['date'], 0) + post.get('uv2',0)
            else:
                uv1[post['date']] = uv1.get(post['date'], 0) + post.get('uv1',0)
                uv2[post['date']] = uv2.get(post['date'], 0) + post.get('uv2',0)
           
        print uv1
        print uv2

        luv1 = list(uv1)
        luv1.sort()
        
        luv2 = list(uv2)
        luv2.sort()
        
        xml_head=ur"<graph caption=' {0}年{1}月 UV数据报表图' xAxisName='Day' yAxisName='UV' showValues='1' showLabels='1'\
        numberPrefix=''> ".format(_time[:4], _time[4:])
        xml_categories = '<categories>'
        for i in range(len(luv1)):
            xml_categories += "<category Name='{0}' />".format((luv1[i])[6:])
        xml_categories += '</categories>'
        
        xml_dataset1 = ur"<dataset seriesName='非UV2.0客户端' color='8BBA00'>"
        for i in range(len(luv1)):
            xml_dataset1 += "<set value='{0}' />".format(uv1[luv1[i]])
        xml_dataset1 += "</dataset>"
        
        xml_dataset2 = ur"<dataset seriesName='UV2.0客户端' color='8E468E'>"
        for i in range(len(luv2)):
            xml_dataset2 += "<set value='{0}' />".format(uv2[luv2[i]])
        xml_dataset2 += "</dataset>"
        
        xml_trendlines = ur"<trendlines>\
        <line startValue='1200000' color='91C728' displayValue='Target' showOnTop='1'/>\
        </trendlines>"
        
        xml_footer = '</graph>'
        xmlStr = xml_head+xml_categories+xml_dataset1+xml_dataset2+xml_trendlines+xml_footer
        
        self.render('templates/client-uv.html', uv1=uv1, uv2=uv2, keys=luv1, add=add, xmlStr=xmlStr, time=_time, platform=_platform )

class VV(RequestHandler):
    def get(self): 
        _time = self.get_argument('time', '').strip()
        _platform = self.get_argument('platform', '').strip()
        if not _time:
            _time = '201111'
        if not _platform:
            _platform = 'all'
        connection = pymongo.Connection("10.103.13.42", 27017)
        db = connection.DailyLog
        collection = db[_time]
        posts = collection.find()

        print dir(posts)
        _posts = []
        android_vv2 = {}
        ios_vv2 = {}
        compiles_android = re.compile('android', re.I)
        compiles_ios = re.compile('(ios|iphone|ipad)', re.I)
        for post in posts:
            pid = post['pid']
            
            datamatch_android = compiles_android.search(post.get('keyword',''))
            datamatch_ios = compiles_ios.search(post.get('keyword',''))
            
            if datamatch_android:
                android_vv2[post['date']] = android_vv2.get(post['date'], 0) + post.get('vv2',0)
            elif datamatch_ios:
                ios_vv2[post['date']] = ios_vv2.get(post['date'], 0) + post.get('vv2',0)
            else:
                pass

        

        lvv_ios = list(ios_vv2)
        lvv_ios.sort()
        
        lvv_android = list(android_vv2)
        lvv_android.sort()
        
        xml_head=ur"<graph caption=' {0}年{1}月 2.0客户端 VV数据报表图' xAxisName='Day' yAxisName='UV' showValues='1' showLabels='1'\
        numberPrefix=''> ".format(_time[:4], _time[4:])
        xml_categories = '<categories>'
        for i in range(len(lvv_ios)):
            xml_categories += "<category Name='{0}' />".format((lvv_ios[i])[6:])
        xml_categories += '</categories>'
        
        xml_dataset1 = ur"<dataset seriesName='IOS平台vv' color='8BBA00'>"
        for i in range(len(lvv_ios)):
            xml_dataset1 += "<set value='{0}' />".format(ios_vv2[lvv_ios[i]])
        xml_dataset1 += "</dataset>"
        
        xml_dataset2 = ur"<dataset seriesName='Android平台vv' color='8E468E'>"
        for i in range(len(lvv_android)):
            xml_dataset2 += "<set value='{0}' />".format(android_vv2[lvv_android[i]])
        xml_dataset2 += "</dataset>"
        
        xml_trendlines = ur"<trendlines>\
        <line startValue='1000000' color='91C728' displayValue='Target' showOnTop='1'/>\
        </trendlines>"
        
        xml_footer = '</graph>'
        xmlStr = xml_head+xml_categories+xml_dataset1+xml_dataset2+xml_trendlines+xml_footer
        
        self.render('templates/client-vv.html', android_vv2=android_vv2, ios_vv2=ios_vv2, keys=lvv_ios, xmlStr=xmlStr, time=_time, platform=_platform )

class PID(RequestHandler):
    def get(self): 
        _time = self.get_argument('time', '').strip()
        _pid = self.get_argument('pid', '').strip()
        
        #read_pid()

        if not _pid:            
            self.render('templates/client-pid.html', client_pid=client_pid, pid='', time=_time, pv={}, uv={}, vv={}, xmlStr='')
            return
        if not _time:
            _time = '201111'
        
        connection = pymongo.Connection("10.103.13.42", 27017)
        db = connection.DailyLog
        collection = db[_time]
        posts = collection.find({'pid':_pid}) 
        
        _posts = []
        pv = {}
        uv = {}
        vv = {}
        for post in posts:
            _posts.append( post )
            pv[post['date']] = pv.get(post['date'], 0) + post['pv']
            uv[post['date']] = uv.get(post['date'], 0) + post.get('uv1',0) + post.get('uv2',0)
            vv[post['date']] = vv.get(post['date'], 0) + post.get('vv2',0)
        
        pid_list = list(pv)
        pid_list.sort()
        
        xml_head=ur"<graph caption=' {0}年{1}月 渠道数据报表图' xAxisName='Day' yAxisName='UV' showValues='1' showLabels='1'\
        numberPrefix=''> ".format(_time[:4], _time[4:])
        xml_categories = '<categories>'
        for i in range(len(pid_list)):
            xml_categories += "<category Name='{0}' />".format((pid_list[i])[6:])
        xml_categories += '</categories>'
        
        xml_dataset1 = ur"<dataset seriesName='pv' color='8BBA00'>"
        for i in range(len(pid_list)):
            xml_dataset1 += "<set value='{0}' />".format(pv[pid_list[i]])
        xml_dataset1 += "</dataset>"
        
        xml_dataset2 = ur"<dataset seriesName='uv' color='8E468E'>"
        for i in range(len(pid_list)):
            xml_dataset2 += "<set value='{0}' />".format(uv[pid_list[i]])
        xml_dataset2 += "</dataset>"
        
        xml_dataset3 = ur"<dataset seriesName='vv' color='A186BE'>"
        for i in range(len(pid_list)):
            xml_dataset3 += "<set value='{0}' />".format(vv[pid_list[i]])
        xml_dataset3 += "</dataset>"
        
        
        xml_footer = '</graph>'
        xmlStr = xml_head+xml_categories+xml_dataset1+xml_dataset2+xml_dataset3+xml_footer
        self.render('templates/client-pid.html', client_pid=client_pid, pid=_pid, keys=pid_list, time=_time, pv=pv, uv=uv, vv=vv, xmlStr=xmlStr)

class AndroidQudao(RequestHandler):
    def get(self): 
        _time = self.get_argument('time', '').strip()
        _pid = self.get_argument('pid', '').strip()
        
        read_pid()

        if not _pid:            
            self.render('templates/client-pid.html', client_pid=client_pid, pid='', time=_time, pv={}, uv={}, vv={}, xmlStr='')
            return
        if not _time:
            _time = '201111'
        
        connection = pymongo.Connection("10.103.13.42", 27017)
        db = connection.DailyLog
        collection = db[_time]
        posts = collection.find({'pid':_pid}) 
        
        _posts = []
        pv = {}
        uv = {}
        vv = {}
        for post in posts:
            _posts.append( post )
            pv[post['date']] = pv.get(post['date'], 0) + post['pv']
            uv[post['date']] = uv.get(post['date'], 0) + post.get('uv1',0) + post.get('uv2',0)
            vv[post['date']] = vv.get(post['date'], 0) + post.get('vv2',0)
        
        pid_list = list(pv)
        pid_list.sort()
        
        xml_head=ur"<graph caption=' {0}年{1}月 渠道数据报表图' xAxisName='Day' yAxisName='UV' showValues='1' showLabels='1'\
        numberPrefix=''> ".format(_time[:4], _time[4:])
        xml_categories = '<categories>'
        for i in range(len(pid_list)):
            xml_categories += "<category Name='{0}' />".format((pid_list[i])[6:])
        xml_categories += '</categories>'
        
        xml_dataset1 = ur"<dataset seriesName='pv' color='8BBA00'>"
        for i in range(len(pid_list)):
            xml_dataset1 += "<set value='{0}' />".format(pv[pid_list[i]])
        xml_dataset1 += "</dataset>"
        
        xml_dataset2 = ur"<dataset seriesName='uv' color='8E468E'>"
        for i in range(len(pid_list)):
            xml_dataset2 += "<set value='{0}' />".format(uv[pid_list[i]])
        xml_dataset2 += "</dataset>"
        
        xml_dataset3 = ur"<dataset seriesName='vv' color='A186BE'>"
        for i in range(len(pid_list)):
            xml_dataset3 += "<set value='{0}' />".format(vv[pid_list[i]])
        xml_dataset3 += "</dataset>"
        
        
        xml_footer = '</graph>'
        xmlStr = xml_head+xml_categories+xml_dataset1+xml_dataset2+xml_dataset3+xml_footer
        self.render('templates/client-pid.html', client_pid=client_pid, pid=_pid, keys=pid_list, time=_time, pv=pv, uv=uv, vv=vv, xmlStr=xmlStr)
      
class Daily(RequestHandler):
    def get(self): 
        _time = self.get_argument('time', '').strip()
        _pid = self.get_argument('pid', '').strip()
        _platform = self.get_argument('platform', '').strip()
        
        #read_pid()
        #print client_pid

        if not _time:
            _time = '201111'
        
        connection = pymongo.Connection("10.103.13.42", 27017)
        db = connection.DailyLog
        collection = db[_time]
        if  _pid:
            posts = collection.find({'pid':_pid}) 
        else :
            _pid=''
            posts = collection.find() 
            #--------------------------------------------------------------- all
            # self.render('templates/client-pid.html', client_pid=client_pid, pid='', time=_time, pv={}, uv={}, vv={}, xmlStr='')
            #------------------------------------------------------------ return
        _posts = []
        pv = {}
        uv = {}
        uv1 = {}
        uv2 = {}
        vv1 = {}
        vv2 = {}
        vv = {}
        compiles_android = re.compile('android', re.I)
        compiles_ios = re.compile('(ios|iphone|ipad)', re.I)
        compiles_other = re.compile('^((?!android|ios|iphone|ipad|wap).)*$', re.I)
        for post in posts:
            pid = post['pid']
            if pid == '127f7a5e3a366bd0' or pid == '53ef315e3bad9e03' or pid == 'a8c022bcd2adcbee':
                continue
            
            keyword = post.get('keyword','')
            
            if _platform : 
                if _platform == 'android':
                    datamatch = compiles_android.search(keyword)
                    if not datamatch:
                        continue
                elif _platform == 'ios':
                    datamatch = compiles_ios.search(keyword)
                    if not datamatch:
                        continue
                elif _platform == 'wap':
                    if 'wap' != post['server']:
                        continue
                elif _platform == 'other':
                    datamatch = compiles_other.search(keyword)
                    if not datamatch:
                        continue
                else:
                    pass
            
            pv[post['date']] = pv.get(post['date'], 0) + int(post.get('pv',0))
            uv[post['date']] = int(uv.get(post['date'], 0)) + int(post.get('uv1',0)) + int(post.get('uv2',0))
            uv1[post['date']] = int(uv1.get(post['date'], 0)) + int(post.get('uv1',0))
            uv2[post['date']] = int(uv2.get(post['date'], 0)) + int(post.get('uv2',0))  
            vv[post['date']] = int(vv.get(post['date'], 0)) + int(post.get('vv1',0)) + int(post.get('vv2',0))
            vv1[post['date']] = int(vv1.get(post['date'], 0)) + int(post.get('vv1',0))
            vv2[post['date']] = int(vv2.get(post['date'], 0)) + int(post.get('vv2',0))  
        
        pid_list = list(pv)
        pid_list.sort()
        
        xml_head=ur"<graph caption=' {0}年{1}月 日常统计报表图' xAxisName='Day' yAxisName='UV' showValues='1' showLabels='1'\
        numberPrefix=''> ".format(_time[:4], _time[4:])
        xml_categories = '<categories>'
        for i in range(len(pid_list)):
            xml_categories += "<category Name='{0}' />".format((pid_list[i])[6:])
        xml_categories += '</categories>'
        
        xml_dataset1 = ur"<dataset seriesName='pv' color='8BBA00'>"
        for i in range(len(pid_list)):
            xml_dataset1 += "<set value='{0}' />".format(pv[pid_list[i]])
        xml_dataset1 += "</dataset>"
        
        xml_dataset2 = ur"<dataset seriesName='uv' color='8E468E'>"
        for i in range(len(pid_list)):
            xml_dataset2 += "<set value='{0}' />".format(uv[pid_list[i]])
        xml_dataset2 += "</dataset>"
        
        xml_dataset3 = ur"<dataset seriesName='vv' color='A186BE'>"
        for i in range(len(pid_list)):
            xml_dataset3 += "<set value='{0}' />".format(vv[pid_list[i]])
        xml_dataset3 += "</dataset>"
        
        
        xml_footer = '</graph>'
        xmlStr = xml_head+xml_categories+xml_dataset1+xml_dataset2+xml_dataset3+xml_footer
        
        pid_list.sort(None,None,True)
        self.render('templates/client-daily.html', client_pid=client_pid, platform=_platform, pid=_pid, keys=pid_list, time=_time, pv=pv, uv=uv, vv=vv,uv1=uv1, uv2=uv2, vv1=vv1, vv2=vv2, xmlStr=xmlStr)

class DailyV2(RequestHandler):
    def get(self): 
        _time = self.get_argument('time', '').strip()
        _pid = self.get_argument('pid', '').strip()
        _platform = self.get_argument('platform', '').strip()
        _database = self.get_argument('database', '').strip()
        
        read_android_pid()
        #print client_pid

        if not _time:
            _time = '201111'
        if not _platform:
            _platform = 'all'
        
        connection = pymongo.Connection("10.103.13.42", 27017)
        db = connection.DailyLog
        if _database and _database == 'backup':
            db = connection.DailyLogBackup
           
        collection = db[_time]
        if  _pid:
            posts = collection.find({'server':'api','pid':_pid}) 
        else :
            _pid=''
            posts = None
            if _platform == 'wap':
                posts = collection.find({'server':'wap'})
            else:
                posts = collection.find({'server':'api'}) 
            #--------------------------------------------------------------- all
            # self.render('templates/client-pid.html', client_pid=client_pid, pid='', time=_time, pv={}, uv={}, vv={}, xmlStr='')
            #------------------------------------------------------------ return
        _posts = []
        pv = {}
        uv = {}
        uv1 = {}
        uv2 = {}
        vv1 = {}
        vv2 = {}
        vv = {}
        new_user = {}
        compiles_android = re.compile('android', re.I)
        compiles_ios = re.compile('(ios|iphone|ipad)', re.I)
        compiles_other = re.compile('^((?!android|ios|iphone|ipad|wap).)*$', re.I)
        for post in posts:
            pid = post['pid']
            if pid == '127f7a5e3a366bd0' or pid == '53ef315e3bad9e03' or pid == 'a8c022bcd2adcbee':
                continue
            
            keyword = post.get('keyword','')
            
            if _platform : 
                if _platform == 'android':
                    datamatch = compiles_android.search(keyword)
                    if not datamatch:
                        continue
                    if not post['pid'] or post['pid'] not in android_phone_pid:
                        continue
                elif _platform == 'ios':
                    datamatch = compiles_ios.search(keyword)
                    if not datamatch:
                        continue
                elif _platform == 'wap':
                    if 'wap' != post['server']:
                        continue
                elif _platform == 'other':
                    datamatch = compiles_other.search(keyword)
                    if not datamatch:
                        continue
                else:
                    pass
            
            pv[post['date']] = pv.get(post['date'], 0) + int(post.get('pv',0))
            uv[post['date']] = int(uv.get(post['date'], 0)) + int(post.get('uv1',0)) + int(post.get('uv2',0))
            uv1[post['date']] = int(uv1.get(post['date'], 0)) + int(post.get('uv1',0))
            uv2[post['date']] = int(uv2.get(post['date'], 0)) + int(post.get('uv2',0))  
            vv[post['date']] = int(vv.get(post['date'], 0)) + int(post.get('vv1',0)) + int(post.get('vv2',0))
            vv1[post['date']] = int(vv1.get(post['date'], 0)) + int(post.get('vv1',0))
            vv2[post['date']] = int(vv2.get(post['date'], 0)) + int(post.get('vv2',0))
            new_user[post['date']] = int(new_user.get(post['date'], 0)) + int(post.get('new_user2',0))  
        
        pid_list = list(pv)
        pid_list.sort()
        
        xml_head=ur"<graph caption=' {0}年{1}月 日常统计报表图' xAxisName='Day' yAxisName='UV' showValues='1' showLabels='1'\
        numberPrefix=''> ".format(_time[:4], _time[4:])
        xml_categories = '<categories>'
        for i in range(len(pid_list)):
            xml_categories += "<category Name='{0}' />".format((pid_list[i])[6:])
        xml_categories += '</categories>'
        
        xml_dataset1 = ur"<dataset seriesName='pv' color='8BBA00'>"
        for i in range(len(pid_list)):
            xml_dataset1 += "<set value='{0}' />".format(pv[pid_list[i]])
        xml_dataset1 += "</dataset>"
        
        xml_dataset2 = ur"<dataset seriesName='uv' color='8E468E'>"
        for i in range(len(pid_list)):
            xml_dataset2 += "<set value='{0}' />".format(uv[pid_list[i]])
        xml_dataset2 += "</dataset>"
        
        xml_dataset3 = ur"<dataset seriesName='vv' color='A186BE'>"
        for i in range(len(pid_list)):
            xml_dataset3 += "<set value='{0}' />".format(vv[pid_list[i]])
        xml_dataset3 += "</dataset>"
        
        
        xml_footer = '</graph>'
        xmlStr = xml_head+xml_categories+xml_dataset1+xml_dataset2+xml_dataset3+xml_footer
        
        pid_list.sort(None,None,True)
        self.render('templates/client-daily-v2.html', client_pid=client_pid, platform=_platform, pid=_pid, keys=pid_list, time=_time, pv=pv, uv=uv, vv=vv,uv1=uv1, uv2=uv2, vv1=vv1, vv2=vv2, new_user=new_user, xmlStr=xmlStr)

class Channel(RequestHandler):
    def get(self): 
        resjson = clientcache.getPromoFactoryName()
        _time = self.get_argument('time', '').strip()
        _fid = self.get_argument('fid', '').strip()
        
        if not _fid:            
            self.render('templates/client-channel.html', pid_data={}, client_pid=client_pid, fid='', time=_time, pv={}, uv={}, vv={}, xmlStr='', resjson=resjson.get('items'))
            return
        
        if not _time:
            _time = '201201'
        
        pid = []
        db = connectMySQL.connMySQL_mobileProduction()
        c=db.cursor()        
        sql = "SELECT pid  FROM `s_partner_new` WHERE  f_id={0}".format(_fid)
        #print sql
        c.execute(sql)  
        res= c.fetchall()
        _tmpdate = ''
        for x in res:
            pid.insert(0,{'pid':x[0]})
        
        c.close()
        db.close()
        
        #print pid
        connection = pymongo.Connection("10.103.13.42", 27017)
        db1 = connection.DailyLog
        collection = db1[_time]
        posts = collection.find({'$or':pid}) 
        
        pid_data = {}

        for post in posts:
            #pid_data[post['pid']] = {post['date']:{'pv':post['pv'], 'uv':post.get('uv1',0)+post.get('uv2',0), 'vv':post.get('vv1',0)+post.get('vv2',0)}}
            o_pid = pid_data.get(post['pid'], {})
            o_time = o_pid.get(post['date'], {})
            o_time['pv'] = o_time.get('pv', 0) + post.get('pv', 0)
            o_time['uv1'] = o_time.get('uv1', 0) + post.get('uv1', 0) 
            o_time['uv2'] = o_time.get('uv2', 0) + post.get('uv2', 0)
            o_time['vv1'] = o_time.get('vv1', 0) + post.get('vv1', 0)
            o_time['vv2'] = o_time.get('vv2', 0) + post.get('vv2', 0)
            o_pid[post['date']] = o_time
            pid_data[post['pid']] = o_pid

        

        for pid, data in pid_data.items():
            datelist = pid_data[pid].keys()
            datedata = pid_data[pid].values()
            datelist.sort()
            pid_data_new = []
            for datestr in datelist:
                pid_data_new.insert(0, {datestr:pid_data[pid][datestr]})
            pid_data[pid] = pid_data_new
        
        #print json.dumps(pid_data)
        
        self.render('templates/client-channel.html', pid_data=pid_data, client_pid=client_pid, fid=_fid, keys=pid_data, time=_time, pv='', uv='', vv='', xmlStr='', resjson=resjson.get('items'))

class ClientStatWAP(RequestHandler):
    def get(self): 
        _time = self.get_argument('time', '').strip()
        if not _time:
            _time = '201110'
        connection = pymongo.Connection("10.103.13.15", 27017)
        db = connection.statictisnew
        collection = db[_time]
        posts = collection.find().sort('date', pymongo.ASCENDING)
        
        _posts = []
        for post in posts:
            _posts.append( post )
        
        self.render('templates/clientstatictis-wap.html', content=_posts)

class SERVERPV(RequestHandler):
    def get(self): 
        _time = self.get_argument('time', '').strip()
        _platform = self.get_argument('platform', '').strip()
        _server = self.get_argument('server', '').strip()
        if not _time:
            _time = '201110'
        if not _platform:
            _platform = 'all'
        connection = pymongo.Connection("10.103.13.42", 27017)
        db = connection.DailyLog
        collection = db[_time]
        if not _server:
            posts = collection.find()
        else:
            posts = collection.find({'server' : _server})

        print dir(posts)
        _posts = []
        pv = {}
        compiles_android = re.compile('android', re.I)
        compiles_ios = re.compile('(ios|iphone|ipad)', re.I)
        for post in posts:
            pid = post['pid']
            if _platform == 'android':
                print post.get('keyword','')
                datamatch = compiles_android.search(post.get('keyword',''))
                if datamatch:
                    pv[post['date']] = pv.get(post['date'], 0) + post['pv']
            elif _platform == 'ios':
                datamatch = compiles_ios.search(post.get('keyword',''))
                if datamatch:
                    pv[post['date']] = pv.get(post['date'], 0) + post['pv']
            else:
                pv[post['date']] = pv.get(post['date'], 0) + post['pv']
           
        print pv
#        for a in pv.items():
#            print dir(a)
        lpv = list(pv)
        lpv.sort()
        
        #xmlStr = render.xml(pv)
        xml_head=ur"<graph caption=' {0} PV数据报表图' xAxisName='Day' yAxisName='PV' \
        showValues='1' lineThickness='1' animation='1' alpha='100' showLimits='1' decimalPrecision='1' rotateNames='1' numDivLines='3'\
        bgcolor='F3f3f3' bgAlpha='70' \
        showColumnShadow='0' divlinecolor='c5c5c5' divLineAlpha='80' \
        showAlternateHGridColor='1' alternateHGridColor='f8f8f8' alternateHGridAlpha='80' > ".format(_time)
        xml_content = ''
        for i in range(len(lpv)):
            xml_content += "<set name='{0}' value='{1}' color='{2}'/>".format(lpv[i], pv[lpv[i]], color[i])
        xml_footer = '''</graph>'''
        xmlStr = xml_head+xml_content+xml_footer
        
        self.render('templates/client-server-pv.html', content=pv, keys=lpv, add=add, xmlStr=xmlStr)
        
def read_pid():
    global client_pid
    f = codecs.open('./android2-pid', 'r', encoding=_encoding, errors=_errors)
    row = f.readline()
    while row:
        _row = row.strip().split(',')
        if len(_row) <2 :
            row= f.readline()
            continue
        client_pid[_row[1]] = _row[0]
        row = f.readline()

def read_android_pid():
    global android_pad_pid
    global android_phone_pid
    f = codecs.open('./PID_android', 'r', encoding=_encoding, errors=_errors)
    row = f.readline()
    while row:
        _row = row.strip().split('^_^')
        if len(_row) <5 :
            row= f.readline()
            continue
        if _row[4] and _row[4] == 'Phone':
            android_phone_pid.append(_row[0])
        if _row[4] and _row[4] == 'Pad':
            android_pad_pid.append(_row[0])
        row = f.readline()